#!/bin/sh

################################################################################
#    Copyright (c) 2017, 2025 Eurotech
#
#    All rights reserved. This program and the accompanying materials
#    are made available under the terms of the Eclipse Public License v1.0
#    which accompanies this distribution, and is available at
#    http://www.eclipse.org/legal/epl-v10.html
#
################################################################################

# Configure credentials

: ARTEMIS_USER=${ARTEMIS_USER:=kapua-sys}
: ARTEMIS_PASS=${ARTEMIS_PASS:=kapua-password}

sh
#/opt/artemis/bin/artemis user add --user ${ARTEMIS_USER} --password ${ARTEMIS_PASS} --role amq

# Continue with startup
cd /opt/artemis/

/opt/artemis/bin/artemis create \
    --allow-anonymous \
    --aio \
    --autocreate \
    --cluster-password password \
    --cluster-user user \
    --data ./data/kapua \
    --default-port 1883 \
    --home /opt/artemis \
    --host ${BINDING_IP:-0.0.0.0} \
    --name kapua \
    --password password \
    --user user \
    --verbose kapua

echo 'copying configuration...'
cp /opt/artemis/base-configuration/* /opt/artemis/kapua/etc
echo 'copying configuration... DONE'

echo 'copying libraries...'
cp /opt/artemis/base-lib/* /opt/artemis/kapua/lib
echo 'copying libraries... DONE'

#options for DB connection
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.jdbc.database.target=${DB_TARGET:-}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.connection.host=${DB_HOST:-db}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.name=${DB_NAME:-kapuadb}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.connection.port=${DB_PORT:-3306}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.schema=${DB_SCHEMA_NAME:-kapuadb}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.username=${DB_USERNAME:-kapua}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.password=${DB_PASSWORD:-kapua}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.connection.scheme=${DB_CONNECTION_SCHEME:-jdbc:h2:tcp}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.jdbcConnectionUrlResolver=${DB_RESOLVER:-DEFAULT}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.jdbc.driver=${DB_DRIVER:-org.h2.Driver}"
JAVA_ARGS="${JAVA_ARGS} -Dcommons.db.connection.additionalOptions=${DB_CONNECTION_ADDITIONAL_OPTIONS}"

echo 'starting Artemis...'
/opt/artemis/kapua/bin/artemis run xml:/opt/artemis/kapua/etc/bootstrap.xml